import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * @date 2018-06-25
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import eventService from "../services/eventService";
export default {
  namespace: 'event',
  state: {
    queryValue: {},
    modalVisible: false,
    ruleModalVisible: false,
    list: {},
    messageList: {},
    // 消息列表
    messageTypeCode: [],
    // 消息类型
    pagination: {} // 分页信息对象

  },
  effects: {
    query: /*#__PURE__*/_regeneratorRuntime.mark(function query(_ref, _ref2) {
      var payload, call, put, response, list, pagination;
      return _regeneratorRuntime.wrap(function query$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(eventService.queryEvents, payload);

            case 4:
              response = _context.sent;
              list = getResponse(response);
              pagination = createPagination(list);

              if (!list) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  list: list,
                  pagination: pagination
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, query);
    }),
    getEvent: /*#__PURE__*/_regeneratorRuntime.mark(function getEvent(_ref3, _ref4) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function getEvent$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(eventService.getEvent, payload);

            case 4:
              response = _context2.sent;
              return _context2.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, getEvent);
    }),
    updateEvent: /*#__PURE__*/_regeneratorRuntime.mark(function updateEvent(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function updateEvent$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(eventService.updateEvent, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, updateEvent);
    }),
    updateRule: /*#__PURE__*/_regeneratorRuntime.mark(function updateRule(_ref7, _ref8) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function updateRule$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(eventService.updateRule, payload);

            case 4:
              response = _context4.sent;
              return _context4.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, updateRule);
    }),
    action: /*#__PURE__*/_regeneratorRuntime.mark(function action(_ref9, _ref10) {
      var method, payload, call, response;
      return _regeneratorRuntime.wrap(function action$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              method = _ref9.method, payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(eventService[method], payload);

            case 4:
              response = _context5.sent;
              return _context5.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, action);
    }),
    // 查询消息
    getMessageList: /*#__PURE__*/_regeneratorRuntime.mark(function getMessageList(_ref11, _ref12) {
      var payload, call, put, response, list;
      return _regeneratorRuntime.wrap(function getMessageList$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call, put = _ref12.put;
              _context6.next = 4;
              return call(eventService.queryMessages, payload);

            case 4:
              response = _context6.sent;
              list = getResponse(response);

              if (!list) {
                _context6.next = 9;
                break;
              }

              _context6.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  messageList: list
                }
              });

            case 9:
            case "end":
              return _context6.stop();
          }
        }
      }, getMessageList);
    }),
    // 获取消息类型
    fetchMessageTypeCode: /*#__PURE__*/_regeneratorRuntime.mark(function fetchMessageTypeCode(_, _ref13) {
      var call, put, messageTypeCode;
      return _regeneratorRuntime.wrap(function fetchMessageTypeCode$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              call = _ref13.call, put = _ref13.put;
              _context7.t0 = getResponse;
              _context7.next = 4;
              return call(queryIdpValue, 'CMSG.MESSAGE_TYPE');

            case 4:
              _context7.t1 = _context7.sent;
              messageTypeCode = (0, _context7.t0)(_context7.t1);
              _context7.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  messageTypeCode: messageTypeCode
                }
              });

            case 8:
            case "end":
              return _context7.stop();
          }
        }
      }, fetchMessageTypeCode);
    }),
    // 添加消息
    createMessage: /*#__PURE__*/_regeneratorRuntime.mark(function createMessage(_ref14, _ref15) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function createMessage$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call;
              _context8.next = 4;
              return call(eventService.createMessage, payload);

            case 4:
              result = _context8.sent;
              return _context8.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context8.stop();
          }
        }
      }, createMessage);
    }),
    // 更新消息
    updateMessage: /*#__PURE__*/_regeneratorRuntime.mark(function updateMessage(_ref16, _ref17) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function updateMessage$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload;
              call = _ref17.call;
              _context9.next = 4;
              return call(eventService.updateMessage, payload);

            case 4:
              result = _context9.sent;
              return _context9.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, updateMessage);
    }),
    // 批量删除消息
    deleteMessages: /*#__PURE__*/_regeneratorRuntime.mark(function deleteMessages(_ref18, _ref19) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function deleteMessages$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref18.payload;
              call = _ref19.call;
              _context10.next = 4;
              return call(eventService.deleteMessages, payload);

            case 4:
              response = _context10.sent;
              return _context10.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, deleteMessages);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};